package com.friends.utils.string;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**
 * 字符串工具类
 * @author lihao
 **/
@Slf4j
public class StringUtils {


    /**
     * 驼峰格式字符串转下划线
     */
    public static String camelToUnderscore(String camelStr) {
        StringBuilder result = new StringBuilder();
        if (camelStr != null && camelStr.length() > 0) {
            for (int i = 0; i < camelStr.length(); i++) {
                char str = camelStr.charAt(i);
                if (Character.isUpperCase(str)) {
                    result.append("_");
                    result.append(String.valueOf(str).toLowerCase());
                } else {
                    result.append(str);
                }
            }
        }
        return result.toString();
    }

    /**
     * 对象转Map
     * @param obj
     * @return
     * @throws IllegalAccessException
     */
    @SuppressWarnings("deprecation")
    public static Map<String, Object> entityToMap(Object obj) throws IllegalAccessException {
        Map<String, Object> map = new HashMap<>(16);
        for (Field field : obj.getClass().getDeclaredFields()) {
            boolean flag = field.isAccessible();
            field.setAccessible(true);
            Object o = field.get(obj);
            map.put(field.getName(), o);
            field.setAccessible(flag);
        }
        return map;
    }

    /**
     * 分隔字符串 去除空格
     *
     * @param string   源字符串
     * @param splitStr 分隔符
     * @return 结果
     */
    public static List<String> splitStrAndStripBlank(String string, String splitStr) {
        if (Objects.isNull(string)) {
            return Collections.emptyList();
        }
        String[] strs = string.split(splitStr);
        List<String> result = new ArrayList<>(strs.length);
        for (final String str : strs) {
            if (org.apache.commons.lang3.StringUtils.isBlank(str)) {
                continue;
            }
            result.add(str.trim());
        }
        return result;
    }

    /**
     * 分隔字符串 去除空格
     *
     * @param strings  源字符串
     * @param splitStr 分隔符
     * @return 结果
     */
    public static List<String> splitStrsAndStripBlank(List<String> strings, String splitStr) {
        if (CollectionUtils.isEmpty(strings)) {
            return Collections.emptyList();
        }
        List<String> result = new ArrayList<>(strings.size());
        for (final String string : strings) {
            String[] strs = string.split(splitStr);
            for (final String str : strs) {
                if (org.apache.commons.lang3.StringUtils.isBlank(str)) {
                    continue;
                }
                result.add(str.trim());
            }

        }
        return result;
    }

    /**
     * 设置小数点格式
     *
     * @param obj
     * @return
     */
    public static String setScale(BigDecimal obj) {
        if (org.springframework.util.StringUtils.isEmpty(obj)) {
            return "0";
        }
        String result = obj.setScale(0, RoundingMode.DOWN).toString();
        if ("0".equals(result)) {
            String string = obj.setScale(1, RoundingMode.DOWN).toString();
            return "0.0".equals(string) ? result : string;
        }
        //  去掉两位零点
        String string = obj.toString();
        if (!Objects.equals(string.substring(string.indexOf(".") + 1), "00")) {
            return obj.setScale(1, RoundingMode.DOWN).toString();
        }
        return result;
    }

    /**
     * 判断是否存在空值  针对多参数
     *
     * @param obj
     * @return
     */
    public static Boolean isAllEmpty(Object... obj) {
        for (Object o : obj) {
            if (!org.springframework.util.StringUtils.isEmpty(o)) {
                return Boolean.FALSE;
            }
        }
        return Boolean.TRUE;
    }

    /**
     * 判断是否存在null 针对多参数
     *
     * @param obj
     * @return
     */
    public static Boolean isAllNull(Object... obj) {
        for (Object o : obj) {
            if (o != null) {
                return Boolean.FALSE;
            }
        }
        return Boolean.TRUE;
    }

    /**
     * 判断是否存在空值  针对类
     *
     * @param obj
     * @return
     */
    public static Boolean isAllEmpty(Object obj) {
        if (Objects.isNull(obj)) {
            return true;
        }
        boolean flag = true;
        try {
            Class<?> clazz = obj.getClass();
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                Object object = field.get(obj);
                if (object != null) {
                    flag = false;
                    break;
                }
            }
        } catch (IllegalAccessException e) {
            log.info("isAllEmpty error : {}", e);
        }
        return flag;
    }
}
